﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfTestForCore
{
    class MainWindowViewModel : ViewModelBase
    {
        private List<Student> _listSource = new List<Student>();
        public List<Student> ListSource
        {
            get
            {
                return _listSource;
            }
            set
            {
                _listSource = value;
                RaisePropertyChanged(() => ListSource);
            }
        }

        private ObservableCollection<Student> _observableSource = new ObservableCollection<Student>();
        public ObservableCollection<Student> ObservableSource
        {
            get
            {
                return _observableSource;
            }
            set
            {
                _observableSource = value;
                RaisePropertyChanged(() => ObservableSource);
            }
        }

        public RelayCommand ModeifyCommand { get; set; }
        public RelayCommand AddCommand { get; set; }
        public RelayCommand DeleteCommand { get; set; }
        
        public MainWindowViewModel()
        {
            ModeifyCommand = new RelayCommand(Modify);
            AddCommand = new RelayCommand(Add);
            DeleteCommand = new RelayCommand(Delete);
            InitData();
        }

        private void Delete()
        {
            ListSource.RemoveAt(ListSource.Count - 1);
            ObservableSource.RemoveAt(ObservableSource.Count - 1);
            MessageBox.Show("删除成功");
        }

        private void Add()
        {
            Student student = new Student()
            {
                ID = 100,
                Name = "你好",
                Major = "软件工程",
                Age = 8,
                Grade = 18
            };
            ListSource.Add(student);
            ObservableSource.Add(student);
            MessageBox.Show("增加成功");
        }

        private void Modify()
        {
            for (int i = 0; i < ListSource.Count; i++)
            {
                ListSource[i].Major = "软件工程";
            }
            for (int i = 0; i < ObservableSource.Count; i++)
            {
                ObservableSource[i].Major = "软件工程";
            }
            MessageBox.Show("修改成功");
        }

        private void InitData()
        {
            List<Student> students = new List<Student>();
            for (int i = 0; i < 10; i++)
            {
                Student student = new Student()
                {
                    Name = "张" + (i + 1).ToString(),
                    Grade = i + 1,
                    Major = "专业" + (i + 1).ToString(),
                    Age = 10 + i
                };
                students.Add(student);
            }

            ListSource = students;
            ObservableSource = new ObservableCollection<Student>(students);
        }
    }
}
